﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;

namespace ECCSystem.Reports
{
    public partial class frmCSRMailCategoryReport :  Form    
    {
        DataSet objDS = new DataSet();
        BizRule.ClsReports objBRReports;
        BizRule.ClsCommon objCommon;

        BizRule.ClsUserMaintenance objBROperator = new ECCSystem.BizRule.ClsUserMaintenance();

        public frmCSRMailCategoryReport()
        {
            InitializeComponent();
        }

        private void frmCSRMailCategoryReport_Load(object sender, EventArgs e)
        {
            dtpDate.Text = DateTime.Now.ToShortDateString();
            this.BindOperator();
        }

        private void btnCreate_Click(object sender, EventArgs e)
        {
            btnCreate.Enabled = false;

            //open data source
            objBRReports = new ECCSystem.BizRule.ClsReports();
            objDS = (DataSet)objBRReports.GetCRSCategoryReport(dtpDate.Value.Date.ToString("yyyy/MM/dd"),Convert.ToInt32(cboUserName.SelectedValue.ToString()));
            if (objDS.Tables[0].Rows.Count <= 0)
            {
                MessageBox.Show("データ無し。", "ECC Information");
                btnCreate.Enabled = true;
                return;
            }

            //create report
            this.CreateExcel();

            btnCreate.Enabled = true;

        }

        //bind operator into cboUserName
        private void BindOperator()
        {
            DataSet objDSOperator = new DataSet();
            objDSOperator = (DataSet)objBROperator.GetList();
            objDSOperator.Tables[0].Rows.Add("0", "", "", "", "All");
            objDSOperator.Tables[0].DefaultView.Sort = "ID";
            cboUserName.DataSource = objDSOperator.Tables[0];
            cboUserName.DisplayMember = "UserName";
            cboUserName.ValueMember = "ID";
        }

        //excel operation(open,fill,quit)
        private void CreateExcel()
        {
            Excel.ApplicationClass objExlApp = new Excel.ApplicationClass();
            Excel.Workbook theWookbook = null;
            Excel.Worksheet theWookSheet = null;
            objCommon = new ECCSystem.BizRule.ClsCommon();
            try
            {
                objExlApp.Visible = false;

                object objMissing = System.Reflection.Missing.Value;
                StringBuilder strSavePath = new StringBuilder();
                strSavePath.Append("C:\\ECCReport\\");
                //if strSavePath is not existing,create the path
                if (Directory.Exists(strSavePath.ToString()) == false)
                {
                    Directory.CreateDirectory(strSavePath.ToString());
                }
                StringBuilder strTemplatePath = new StringBuilder();
                //Excel template path
                strTemplatePath.Append(Application.StartupPath);
                strTemplatePath.Append("\\CSRMailCategory.xls");
                //if this template is existing or not
                if (File.Exists(strTemplatePath.ToString()) == false)
                {
                    MessageBox.Show("レポートのテンプレートファイルが見つかりません。", "ECC Information");
                    //quit excel object
                    objCommon.QuitExcelApp(objExlApp, theWookbook, theWookSheet, null);
                    System.GC.Collect();
                    btnCreate.Enabled = true;
                    return;
                }
                //open the excel template
                theWookbook = objExlApp.Workbooks.Open(strTemplatePath.ToString(), objMissing, objMissing, objMissing, objMissing, objMissing,
                                         objMissing, objMissing, objMissing, objMissing, objMissing, objMissing, objMissing);
                //get the first sheet
                theWookSheet = (Excel.Worksheet)theWookbook.Worksheets[1];

                //fill data into excel
                theWookSheet.Cells[1, 1] = dtpDate.Value.Date.ToString("yyyy/MM/dd");
                for (int i = 0; i < objDS.Tables[0].Rows.Count; i++)
                {
                    theWookSheet.Cells[i + 3, 1] = objDS.Tables[0].Rows[i][0].ToString();
                    theWookSheet.Cells[i + 3, 2] = objDS.Tables[0].Rows[i][1].ToString();
                    theWookSheet.Cells[i + 3, 3] = objDS.Tables[0].Rows[i][2].ToString();
                    theWookSheet.Cells[i + 3, 4] = objDS.Tables[0].Rows[i][3].ToString();
                    theWookSheet.Cells[i + 3, 5] = float.Parse(objDS.Tables[0].Rows[i][4].ToString()).ToString("F1");

                }

                //set excel report save path and name
                strSavePath.Append("CSRMailCategory" + DateTime.Now.Year.ToString() + DateTime.Now.Month.ToString() + DateTime.Now.Day.ToString());
                strSavePath.Append(DateTime.Now.Hour.ToString() + DateTime.Now.Minute.ToString() + DateTime.Now.Second.ToString() + ".xls");

                //if the report has existed or not
                if (File.Exists(strSavePath.ToString()) == true)
                {
                    MessageBox.Show("このファイルがもう存在しました！", "ECC Information");
                    objCommon.QuitExcelApp(objExlApp, theWookbook, theWookSheet, null);
                    System.GC.Collect();
                    btnCreate.Enabled = true;
                    return;
                }

                //save the report
                theWookbook.SaveAs(strSavePath.ToString(), objMissing, objMissing, objMissing, objMissing, objMissing,
                    Excel.XlSaveAsAccessMode.xlNoChange, objMissing, objMissing, objMissing, objMissing);
                //prompt save success
                MessageBox.Show("操作成功しました。", "ECC Information");

                //quit excel object
                objCommon.QuitExcelApp(objExlApp, theWookbook, theWookSheet, null);
                System.GC.Collect();
                this.Close();
            }
            catch (Exception ex)
            {
                //quit excel object
                objCommon.QuitExcelApp(objExlApp, theWookbook, theWookSheet, null);
                System.GC.Collect();

                //MessageBox.Show(ex.ToString(), "ECC Catch Error");
                btnCreate.Enabled = true;
            }
        }


    }
}